package burubaka.bat;

import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import burubaka.BurubakaConstants;
import burubaka.model.RaceCardHorse;
import burubaka.model.RaceInfo;
import burubaka.service.RaceCardHorseService;
import burubaka.service.RaceInfoService;

public class RaceCardBat {

	private static final Logger log = Logger.getLogger(RaceCardBat.class.getName());
	private RaceCardHorseService raceCardHorseService = new RaceCardHorseService();
	private RaceInfoService raceInfoService = new RaceInfoService();

	public RaceInfo execute() throws Exception {

		RaceInfo raceInfo = raceInfoService.getCurrentRaceInfo();

		// // すでにレース情報が登録されているかどうか判断
		// // まだ登録されていない場合は何もしない
		// if (raceInfo == null || raceInfo.getProgress() !=
		// BurubakaConstants.PROGRESS_NOMINATION) {
		// return;
		// }

		List<RaceCardHorse> list = raceCardHorseService.getRaceCardHorseListByURL(raceInfo);

		if (list.size() == 0) {
			return raceInfo;
		}

		// 開始日取得
		Date startTime = RaceCardHorseService.getStartTime(raceInfo);
		raceInfo.setStartTime(startTime);

		// 締切時間取得
		Date deadline = RaceCardHorseService.getDeadline(raceInfo);

		// レース情報更新
		raceInfo.setDeadline(deadline);
		raceInfo.setProgress(BurubakaConstants.PROGRESS_RACECARD);
		raceInfoService.regist(raceInfo);

		// 登録馬登録
		raceCardHorseService.regist(list, raceInfo);
		return raceInfo;
	}
}
